﻿Imports ISNet.WebUI.WebGrid
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports log4net

Partial Class ProjectData
    Inherits System.Web.UI.Page
    Public ScripText As String = ""

    Private Shared logger As ILog = LogManager.GetLogger("FileAppender")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        ScreenName.Text = "ระบบจัดทำงบประมาณ  >>  จัดทำงบประมาณ >> ข้อมูลหลักโครงการ / งานสนับสนุน"
        'Session("users") = "bb0011"
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        If IsPostBack = False Then
            PlanYearsTxt.Text = Session("PlanYear")
            Session("PlanYearTxt") = PlanYearsTxt.Text
            Session("searchtext") = ""

            'Else
            '    PlanYearsTxt.Text = Session("PlanYearTxt")
        End If

    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs)
        Dim adapter As New B10DataSetTableAdapters.ProjectsTableAdapter
        Dim dtTable As New B10DataSet.ProjectsDataTable
        Dim sql As String = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        If Session("searchtext") = "" And Session("savedata") = "" Then
            e.DataSource = dtTable
            Exit Sub
        ElseIf Session("savedata") <> "" Then
            SearchData()
            Session.Remove("savedata")
        End If

        'If IsPostBack = True And Session("Search") = True Then

        sql = "select * from Projects "
        sql += " WHERE 1 = 1 "
        sql += Session("searchtext")

        sql += " ORDER BY ProjectCode "

        adapter.GetDataByFilter(dtTable, sql)
        e.DataSource = dtTable
        Session("count_data") = dtTable.Rows.Count

        dtTable.Columns("PlanYear").AllowDBNull = True
        dtTable.Columns("ProjectCode").AllowDBNull = True
        dtTable.Columns("Description").AllowDBNull = True

        'Else

        'sql = "select * from Projects "
        'sql += " WHERE 1 = 1 "
        'sql += " and PlanYear like '" & Val(Session("PlanYearTxt")) & "'"

        'If prefil <> "" Then
        '    sql += " AND FundCenter_FundCenterCode IN (" & prefil & ") "
        'End If


        'sql += " ORDER BY ProjectCode "

        'adapter.GetDataByFilter(dtTable, sql)

        'e.DataSource = dtTable
        'Session("count_data") = dtTable.Rows.Count

        'dtTable.Columns("PlanYear").AllowDBNull = True
        'dtTable.Columns("ProjectCode").AllowDBNull = True
        'dtTable.Columns("Description").AllowDBNull = True


        'End If
        ' Response.Write(sql)

    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' don't edit column
        e.Row.Cells(0).ForceNoEdit = True
        e.Row.Cells(1).ForceNoEdit = True
        e.Row.Cells(2).ForceNoEdit = True
    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim Deletet As String = TryCast(Request("id"), String)
            Dim code As String = TryCast(Request("code"), String)
            Dim yr As String = TryCast(Request("year"), String)
            Dim fctr As String = TryCast(Request("fctr"), String)

            Dim adapter As New B10DataSetTableAdapters.ProjectsTableAdapter
            Dim adap2 As New MasterDataSet2TableAdapters.checkuseTableAdapter

            Try
                Dim used As Integer = adap2.CheckProjectFctr(code, Val(yr), fctr)
                If used = 0 Then
                    adapter.DeleteQuery(Deletet)
                    logger.Info("User " & Session("users") & " delete project code " & Deletet)
                Else
                    WebGrid1.ClientAction.Alert("ไม่สามารลบข้อมูลได้เนื่องจากข้อมูลถูกนำไปใช้งานแล้ว")
                End If
            Catch ex As Exception
                WebGrid1.ClientAction.Alert("ไม่สามารลบข้อมูลได้กรุณาติดต่อผู้ดูแลระบบ")
            End Try

            ScripText = ""
            WebGrid1.ClientAction.Refresh()
        End If

    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        If WebCombo3.Value = "" Then
            ScripText = "<script> alert('กรุณาเลือก Fundcenter');</script>"
            Exit Sub
        End If
        SearchData()
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        Session.Remove("Search")
        Session.Add("Search", True)
    End Sub


    Protected Sub WebCombo3_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo3.InitializeDataSource
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYearTxt")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYearTxt"))
        sql += " AND FundCenterHierarchies.FundCenterGroup = 1 "
        sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear")) & " "
        sql += " ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)

        'dt.Rows.Add("", "โครงการกลาง", "1", "0")


        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt

        If dt.Count = 1 Then
            WebCombo3.Value = dt.Item(0).FundCenterCode
        End If

    End Sub

    Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButton1.Click

        ScripText = "<script> popupkrajay3('" & 1 & "','" & PlanYearsTxt.Text & "','" & 0 & "');</script>"
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub

    Private Sub SearchData()
        Session("searchtext") = ""
        'If Session("savedata") <> 1 Then
        '    Session("PlanYearTxt") = Val(PlanYearsTxt.Text)
        'Else
        'End If
       
        Session("searchtext") += " and PlanYear like '" & Val(Session("PlanYearTxt")) & "'"

        If WebCombo3.Value <> "" Then
            Session("searchtext") += " and  FundCenter_FundCenterCode  = " & WebCombo3.Value
        Else
            Session("searchtext") += " and  FundCenter_FundCenterCode  = '" & Session("savedata") & "'"

        End If

    End Sub
End Class
